package com.qianchang.microfinance.view.mine;


import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.MessageCenterBean;
import com.dm.library.widgets.element.DTextView;

import java.util.ArrayList;
import java.util.List;

public class MessageListAdapter extends BaseAdapter {
    private Context mContext;
    private List<MessageCenterBean> messages = new ArrayList<>();

    public MessageListAdapter(Context context) {
        mContext = context;
    }

    public void updateAll(List<MessageCenterBean> messages) {
        if (null != messages) {
            this.messages.clear();
            this.messages = messages;
            notifyDataSetChanged();
        }
    }

    public void addAll(List<MessageCenterBean> messages) {
        if (null != messages) {
            this.messages.addAll(messages);
            notifyDataSetChanged();
        }
    }

    public List<MessageCenterBean> getAllMessage() {
        return messages;
    }

    @Override
    public int getCount() {
        return messages.size();
    }

    @Override
    public MessageCenterBean getItem(int position) {
        return messages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder mViewHolder;
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_message, parent, false);
            mViewHolder = new ViewHolder();
            mViewHolder.tv_title = (DTextView) convertView.findViewById(R.id.tv_title);
            mViewHolder.tv_time = (DTextView) convertView.findViewById(R.id.tv_time);
            mViewHolder.tv_content = (DTextView) convertView.findViewById(R.id.tv_content);
            mViewHolder.tv_dote = (DTextView) convertView.findViewById(R.id.tv_dote);
            mViewHolder.ivArrow = (ImageView) convertView.findViewById(R.id.iv_arrow);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
//
        MessageCenterBean message = messages.get(position);
        /**
         * 控制已读未读
         */

        String noticeStatus = message.getNoticeStatus();
        if ("1".equals(noticeStatus)) {  //已读
            mViewHolder.tv_dote.setVisibility(View.INVISIBLE);
            mViewHolder.tv_content.setTextColor(mContext.getResources().getColor(R.color.common_black_color_9));
            mViewHolder.tv_time.setTextColor(mContext.getResources().getColor(R.color.common_black_color_9));
            mViewHolder.tv_title.setTextColor(mContext.getResources().getColor(R.color.common_black_color_9));
        } else {
            mViewHolder.tv_dote.setVisibility(View.VISIBLE);
            mViewHolder.tv_content.setTextColor(mContext.getResources().getColor(R.color.common_black_color_6));
            mViewHolder.tv_time.setTextColor(mContext.getResources().getColor(R.color.common_black_color_3));
            mViewHolder.tv_title.setTextColor(mContext.getResources().getColor(R.color.common_black_color_3));
        }

        /**
         * 控制展开和收缩
         */
        if (message.isShow()) {
            mViewHolder.ivArrow.setImageResource(R.drawable.btn_down);
            mViewHolder.tv_content.setEllipsize(null); // 展开
            mViewHolder.tv_content.setSingleLine(false);
            mViewHolder.tv_content.setMaxLines(20);
        } else {
            mViewHolder.ivArrow.setImageResource(R.drawable.btn_up);
            mViewHolder.tv_content.setEllipsize(TextUtils.TruncateAt.END); // 收缩
            mViewHolder.tv_content.setMaxLines(1);
            mViewHolder.tv_content.setSingleLine(true);
        }

        mViewHolder.tv_title.setTextContent(message.getTitle());
        mViewHolder.tv_title.setVisibility(TextUtils.isEmpty(message.getTitle()) ? View.INVISIBLE : View.VISIBLE);
        mViewHolder.tv_content.setTextContent(message.getContent());
        mViewHolder.tv_time.setTextContent(String.valueOf(message.getSendTime()));

        return convertView;
    }

    class ViewHolder {
        private DTextView tv_title;
        private DTextView tv_time;
        private DTextView tv_content;
        private DTextView tv_dote;
        private ImageView ivArrow;
    }


    /**
     * 全部已读
     */
    public void readAll() {
        if (messages.size() > 0) {
            for (MessageCenterBean info : messages) {
                info.setNoticeStatus("1");
            }
            notifyDataSetChanged();
        }
    }


    /**
     * 全部删除
     */
    public void deleteAll() {
        messages.clear();
        notifyDataSetChanged();
    }
}
